/* eslint-disable vue/no-unused-components */
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column
                label="序号"
                type="index"
                :index="sequence"
                width="120"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                background
                :current-page="pageParams.page"
                :page-sizes="[1, 2, 3]"
                :page-size="pageParams.pagesize"
                :total="total"
                layout="total, sizes, prev,pager,next, jumper"
                @current-change="hCurrentChange"
                @size-change="hSizeChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹框 -->
      <el-dialog title="添加" :visible.sync="showDialog" width="50%">
        <el-form :model="roleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" style="width: 80%" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" style="width: 80%" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="hSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog
        title="分配权限"
        :visible.sync="showDialogAssign"
      >
        <AssignPermission
          v-if="showDialogAssign"
          :id="curId"
          @close="showDialogAssign=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole, editRole } from '@/api/settings'
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      roles: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      showDialog: false,
      isEdit: false,
      roleForm: {
        name: '',
        description: ''
      },
      showDialogAssign: false,
      curId: ''
    }
  },
  computed: {
    isLastPageFulled() {
      return this.total % this.pageParams.pagesize === 0
    },
    maxPage() {
      return Math.ceil(this.total / this.pageParams.pagesize)
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 数据渲染
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res, '获取所有员工列表')
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 分页器点击事件
    hCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.pageParams.page = curPage
      // 2. 重发请求
      this.loadRoles()
    },
    hSizeChange(curPageSize) {
    //   alert(curPageSize)
      // 1. 更改每页的条数
      this.pageParams.pagesize = curPageSize

      // 2. 重发请求
      this.loadRoles()
    },
    // 删除角色信息
    async doDel(id) {
      // 1. 删除
      const res = await deleteRole(id)
      console.log('删除的结果', res)
      // 2. 提示
      this.$message.success('删除成功')

      if (this.roles.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page < 1) {
          this.pageParams.page = 1
        }
      }
      // 3. 重发请求
      this.loadRoles()
    },
    hDel(id) {
      this.$confirm('确定要删除吗？', '友情提示').then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    // 序列号算法
    sequence(ind) {
    //   console.log(ind)
      return (this.pageParams.page - 1) * this.pageParams.pagesize + 1 + ind
    },
    // 添加角色
    hAdd() {
      this.showDialog = true
      this.isEdit = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        // 跳转到最后一页
        if (this.isLastPageFulled) {
          this.pageParams.page = this.maxPage + 1
          //  让数据总数+1
          this.total++
        } else {
          this.pageParams.page = this.maxPage
        }
        // 重新渲染
        this.loadRoles()
        // 提示添加结果
        this.$message.success('添加成功')
        // 关闭弹框
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    // 编辑角色信息
    hEdit(row) {
      // 显示弹出框
      this.showDialog = true
      // 弹出框为编辑模式
      this.isEdit = true
      // 把角色信息回填到表格
      if (this.isEdit) { this.roleForm = { ...row } }
    },
    async doEdit() {
      // 提交编辑数据
      const res = await editRole(this.roleForm)
      console.log(res)
      //   重新渲染表格
      this.loadRoles()
      //   提示编辑结果
      this.$message.success('编辑成功')
      //   关闭弹框
      this.showDialog = false
    },
    // 提交表单
    hSubmit() {
      // 1.表单校验

      // 提交
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    hAssign(id) {
      this.showDialogAssign = true
      this.curId = id
    }
  }
}
</script>

